package com.ygo.feihua;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.database.Cursor;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.UploadFileListener;
import cn.bmob.v3.exception.BmobException;
import com.ygo.feihua.BmobTable.GuangGao;
import cn.bmob.v3.listener.UpdateListener;
import java.io.File;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.util.Log;
import android.support.v4.util.*;
import com.ygo.feihua.util.*;

public class ShezhiGg1gl extends Activity implements OnClickListener
{
	private ListView gl_lb;
	private Button gl_addgg;
	int poo;
	private Gongju gj;
	Adaptergg1 adp;
	DialogUtils du;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shezhi_gggl);
		csh();
	}
	private void csh(){
		gl_addgg=(Button) findViewById(R.id.gl_addgg);
		gl_lb=(ListView) findViewById(R.id.gl_gglb);

		gj=Gongju.getdx(this);
		du=DialogUtils.getdx(this);
		
		gl_addgg.setOnClickListener(this);

		gl_addgg.setText("添加广告");
		ggcsh();
	}

	public void ggcsh(){
		du.dialogj1(false,"","加载中,请稍等");
		
		BmobQuery<GuangGao> bq=new BmobQuery<GuangGao>();
		bq.order("-createdAt");
		bq.findObjects(new FindListener<GuangGao>(){

				@Override
				public void done(List<GuangGao> p1, BmobException p2)
				{
					if(p2==null){
						adp=new Adaptergg1(ShezhiGg1gl.this,p1);
						gl_lb.setAdapter(adp);

					}else{
						gj.show("查询广告失败,原因为"+p2);
					}
					du.dis();
					// TODO: Implement this method
				}
			});


	}


	@Override
	public void onClick(View p1)
	{
		switch(p1.getId()){
			case R.id.gl_addgg:

				startActivity(new Intent(this,GuanggaoAdd.class));
				break;
		}
		// TODO: Implement this method
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent ddata)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, ddata);
		switch (requestCode)
		{
			case 234:
				if (resultCode == RESULT_OK) {
					final Uri selectedUri = ddata.getData();
					if (selectedUri != null) {
						du.dialogj1(false,"","替换中,请稍等");
						String []imgs={MediaStore.Images.Media.DATA};//将图片URI转换成存储路径  
						Cursor cursor=this.managedQuery(selectedUri, imgs, null, null, null);  
						int index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  
						cursor.moveToFirst();  
						String img_url=cursor.getString(index); 
						final BmobFile bf=new BmobFile(new File(img_url));
						bf.uploadblock(new UploadFileListener(){

								@Override
								public void done(BmobException p1)
								{
									if(p1==null)
									{				
										final int po=adp.getpo();
										GuangGao gg=new GuangGao();
										gg.setGg_tp(bf);
										gg.update(((GuangGao)adp.getItem(po)).getObjectId(),new UpdateListener(){

												@Override
												public void done(BmobException p1)
												{
													if(p1==null){
														gj.show("替换图片成功");
														GuangGao ma = (GuangGao) adp.getItem(po);
														ma.setGg_tp(bf);
														adp.setData(po,ma);
													}else{
														gj.show("替换图片失败,原因为"+p1);
													}
													du.dis();
													// TODO: Implement this method
												}
											});

									}else{
										gj.show("上传图片失败,原因为"+p1);
										du.dis();
									}
									// TODO: Implement this method
								}
							});
						}}
						break;
						}
}
}
